﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Imaging;
using System.IO;

namespace Liang.Util.ImageHelper
{
    /// <summary>
    /// 图片处理工具
    /// </summary>
    public class ImageProcess
    {
        /// <summary>
        /// 将图片文件打包成待传送的字符串数据
        /// </summary>
        /// <param name="strImageFile"></param>
        /// <returns></returns>
        public static string PackageImage(string strImageFile)
        {
            string ret = string.Empty;

            try
            {
                byte[] tmp = ImageTool.ImageToBytes(strImageFile);
                ret = Convert.ToBase64String(tmp);
            }
            catch (Exception e)
            {
                Console.WriteLine(e.Message);
            }

            return ret;
        }

        //add begin by huhaifeng 2016-04-13
        /// <summary>
        /// 将多个图片路径打包成一个数组
        /// </summary>
        /// <param name="strImageFile"></param>
        /// <returns></returns>
        public static string[] PackageImages(string strImageFile)
        {
            if (strImageFile.IndexOf(";") > 0)
            {
                string[] images = strImageFile.Split(';');
                string[] ret = new string[images.Length];
                for (int i = 0; i < images.Length; i++)
                {
                    byte[] tmp = ImageTool.ImageToBytes(images[i]);
                    ret[i] = Convert.ToBase64String(tmp);
                }
                return ret;
            }
            return null;
        }
        //add end by huhaifeng 2014-04-13

        /// <summary>
        /// 保存图片
        /// </summary>
        /// <param name="strImageName"></param>
        /// <param name="strImageData"></param>
        /// <returns></returns>
        public static bool SaveImage(string strImageName, string strImageData)
        {
            try
            {
                byte[] tmp = Convert.FromBase64String(strImageData);
                System.Drawing.Image img = ImageTool.BytesToImage(tmp);
                img.Save(strImageName);
            }
            catch
            {
                return false;
            }

            return true;
        }

        public static bool ImageOsd(string strDstImageFile, Image img, List<string> osdList, Font font, Brush brush, long nQuality = 100)
        {
            try
            {
                Graphics g = Graphics.FromImage(img);

                float y = 0;
                foreach (string osd in osdList)
                {
                    // 计算字符串占用大小
                    SizeF size = g.MeasureString(osd, font);
                    g.DrawString(osd, font, brush, 0, y);
                    y += size.Height;
                }

                // 指定图像压缩质量
                if (0 > nQuality)
                {
                    nQuality = 0;
                }
                if (100 <= nQuality)
                {
                    nQuality = 100;
                }

                EncoderParameter p = new EncoderParameter(System.Drawing.Imaging.Encoder.Quality, nQuality);
                EncoderParameters ps = new EncoderParameters(1);
                ps.Param[0] = p;

                ImageCodecInfo myImageCodecInfo = ImageTool.GetEncoderInfo("image/jpeg");

                img.Save(strDstImageFile, myImageCodecInfo, ps);
            }
            catch
            {
                return false;
            }

            return true;
        }

        /// <summary>
        /// 字符叠加
        /// </summary>
        /// <param name="strImageFile"></param>
        /// <param name="strImageData"></param>
        /// <param name="osdList"></param>
        /// <param name="font"></param>
        /// <param name="brush"></param>
        /// <param name="nQuality"></param>
        /// <returns></returns>
        public static bool ImageOsd(string strDstFile, string strSrcImageFile, List<string> osdList, Font font, Brush brush, long nQuality = 100)
        {
            bool bSuccessed = false;
            Image img = Image.FromFile(strSrcImageFile);
            bSuccessed = ImageOsd(strDstFile, img, osdList, font, brush, nQuality);
            img.Dispose();
            return bSuccessed;
        }

        public static bool ImageOsdEx(string strImageFile, string strImageData, List<string> osdList, Font font, Brush brush, long nQuality = 100)
        {
            try
            {
                byte[] tmp = Convert.FromBase64String(strImageData);
                using (MemoryStream ms = new MemoryStream(tmp))
                {
                    Image img = Image.FromStream(ms);
                    ImageOsd(strImageFile, img, osdList, font, brush, nQuality);
                }
            }
            catch
            {
                return false;
            }

            return true;
        }
    }
}
